﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PortfolioApp.Web.IDataAdapters.Interfaces;
using PortfolioApp.DataModels;
using PortfolioApp.Data;


namespace PortfolioApp.Web.IDataAdapters.Adapters
{
    public class PortfolioAppAdapter : IPortfolioAppAdapter
    {
        public List<Post> GetSome(int index, int pagesize)
        {
            List<Post> partial;
            using (ApplicationDbContext db = new ApplicationDbContext()){
                partial = db.Posts.OrderBy(x=>x.Title).Skip(index*pagesize).Take(pagesize).ToList();
            }
            return partial;
        }
        //Post CRUD
        public int AddPost(DataModels.Post post)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                post = db.Posts.Add(post);
                db.SaveChanges();
            }
            return post.Id;
        }

        public List<DataModels.Post> GetAllPosts()
        {
            List<Post> posts;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                posts = db.Posts.OrderBy(x => x.Title).ToList();
            }
            return posts;
        }

        public DataModels.Post GetPost(int id)
        {
            Post post;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                post = db.Posts.Find(id);
            }
            return post;
        }

        public DataModels.Post UpdatePost(DataModels.Post post)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Post updatingPost = db.Posts.Find(post.Id);
                updatingPost.SetProps(post);
                db.SaveChanges();
            }
            return post;
        }

        public void DeletePost(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Post post = db.Posts.Find(id);
                db.Posts.Remove(post);
                db.SaveChanges();
            }
        }

        //Project CRUD
        public int AddProject(DataModels.Project project)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                project = db.Projects.Add(project);
                db.SaveChanges();
            }
            return project.Id;
        }

        public List<DataModels.Project> GetAllProjects()
        {
            List<Project> projects;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                projects = db.Projects.OrderBy(x => x.Name).ToList();
            }
            return projects;
        }

        public DataModels.Project GetProject(int id)
        {
            Project project;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                project = db.Projects.Find(id);
            }
            return project;
        }

        public DataModels.Project UpdateProject(DataModels.Project project)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Project updatingProject = db.Projects.Find(project.Id);
                updatingProject.SetProps(project);
                db.SaveChanges();
            }
            return project;
        }

        public void DeleteProject(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Project project = db.Projects.Find(id);
                db.Projects.Remove(project);
                db.SaveChanges();
            }
        }
    }
}